//
//  main.cpp
//  指针的算术运算
//
//  Created by linjinzhu on 2017/7/10.
//  Copyright © 2017年 linjinzhu. All rights reserved.
//

#include <iostream>

using namespace std;

void pIncreasing();
void pDecline();
void pCompare();

int main(int argc, const char * argv[]) {

    pIncreasing();
    cout << "=============" << endl;
    pDecline();
    cout << "=============" << endl;
    pCompare();

    return 0;
}

const int MAX = 3;

void pIncreasing()
{
    int arr[MAX] = {1,2,3};

    int *p = arr;

    for (int i=0; i<MAX; i++) {
        cout << "指针地址：" << p << endl;
        cout << "指针的值：" << *p << endl;
        p++;
    }

    // 指针加1，实际是指针加一个数组元素占用的空间大小
    cout << "单个元素占用空间大小：" << (&arr[1] - & arr[0]) << endl;// 这种写法不对
    cout << "单个元素占用空间大小：" << sizeof(int) << endl;
}

void pDecline()
{
    int arr[MAX] = {1,2,3};

    int *p = &arr[MAX - 1];

    for (int i=MAX; i>0; i--) {
        cout << "指针地址：" << p << endl;
        cout << "指针的值：" << *p << endl;
        p--;
    }
}

void pCompare()
{
    int arr[MAX] = {1,2,3};

    int *p = arr;

    while (p <= &arr[MAX-1]) {
        cout << "指针地址：" << p << endl;
        cout << "指针的值：" << *p << endl;
        p++;
    }
}
